using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using IGlobalist.Cms.Common;
using IGlobalist.Cms.Model;


namespace IGlobalist.Cms.Admin.Control
{


    [ToolboxData("<{0}:CmsPropertyControl runat=server></{0}:CmsPropertyControl>")]
    public class CmsPropertyControl : WebControl, IPostBackDataHandler
    {

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (Page != null)
                Page.RegisterRequiresPostBack(this);
        }

        private BaseProperty _Property;

        public BaseProperty Property
        {
            get
            {
                _Property = (BaseProperty)ViewState["Property"];
                return _Property;
            }
        }

        private System.Web.UI.Control _Control;

        public CmsPropertyControl()
            : base()
        {
        }
        public CmsPropertyControl(BaseProperty property)
            : base()
        {
            ViewState["Property"] = property;
            this.ID = property.Name;
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this._Control = GetControl();
            this.Controls.Clear();
            this.Controls.Add(_Control);
            ViewState["ControlID"] = _Control.UniqueID;
        }


        protected override void RenderContents(HtmlTextWriter output)
        {
            _Control.RenderControl(output);
        }
        private System.Web.UI.Control GetControl()
        {

            _Property = ViewState["Property"] as BaseProperty;
            switch (_Property.InputType)
            {

                case InputType.CheckBox:
                    _Control = new CheckBox();
                    bool isChecked = ("on".Equals("" + _Property.Value) || "true".Equals("" + _Property.Value));
                    (ViewState["Property"] as BaseProperty).Value = isChecked;
                    ((CheckBox)_Control).Checked = isChecked;
                    break;

                case InputType.PasswordField:
                    _Control = new TextBox();

                    ((TextBox)_Control).TextMode = TextBoxMode.Password;
                    ((TextBox)_Control).Text = Convert.ToString(_Property.Value);
                    break;
                case InputType.RadioButtonList:

                    break;
                case InputType.TextBox:
                    _Control = new TextBox();
                    ((TextBox)_Control).Text = Convert.ToString(_Property.Value);
                    break;
                case InputType.TextBoxMultiLine:
                    _Control = new TextBox();
                    ((TextBox)_Control).TextMode = TextBoxMode.MultiLine;
                    ((TextBox)_Control).Text = Convert.ToString(_Property.Value);
                    break;

                default:
                    _Control = new TextBox();
                    break;

            }
            if (_Control != null)
            {
                _Control.ID = _Property.Name + "$" + _Property.Name;
            }
            return _Control;
        }
        public object Value
        {
            get
            {
                return GetValue();
            }
        }

        private object GetValue()
        {

            object value = null;
            switch (_Property.InputType)
            {

                case InputType.CheckBox:
                    value = ((CheckBox)_Control).Checked;
                    break;

                case InputType.PasswordField:
                    value = ((TextBox)_Control).Text;
                    break;
                case InputType.RadioButtonList:

                    break;
                case InputType.TextBox:
                    value = ((TextBox)_Control).Text;
                    break;
                case InputType.TextBoxMultiLine:
                    value = ((TextBox)_Control).Text;
                    break;

                default:
                    value = null;
                    break;

            }
            return value;
        }


        #region IPostBackDataHandler Members

        public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
        {
            string data = postCollection[ViewState["ControlID"] as string];
            if (data == null) data = "";

            if (((IContentProperty)ViewState["Property"]).Value.Equals(data))
                return false;
            ((IContentProperty)ViewState["Property"]).Value = data;
            CreateChildControls();
            return true;
        }

        public void RaisePostDataChangedEvent()
        {
        }

        #endregion
    }


    //[ToolboxData("<{0}:CmsPropertyListControl runat=server></{0}:CmsPropertyListControl>")]
    //public class CmsPropertyListControl : WebControl
    //{

    //    protected override void CreateChildControls()
    //    {           
    //        if (ViewState["PropertyList"] != null)
    //        {
    //            ExtPropertiesParser(ViewState["PropertyList"] as PropertyList);
    //        }
    //    }

    //    public PropertyList PropertyList
    //    {
    //        get
    //        {
    //            return GetValues();
    //        }
    //        set
    //        {
    //            ViewState["PropertyList"] = value;
    //        }
    //    }


    //    private PropertyList GetValues()
    //    {
    //        this.EnsureChildControls();
    //        PropertyList list = (PropertyList)ViewState["PropertyList"];

    //        foreach (BaseProperty property in list)
    //        {
    //            property.Value = ((CmsPropertyControl)this.FindControl(property.Name)).Property.Value;
    //        }

    //        return list;

    //    }

    //    private void ExtPropertiesParser(PropertyList properties)
    //    {
    //        Table tb = new Table();
    //        tb.ID = "tbExtProperties";
    //        foreach (IContentProperty property in properties)
    //        {
    //            tb.Rows.Add(PropertyParser(property));
    //        }

    //        this.Controls.Add(tb);
    //    }

    //    private TableRow PropertyParser(IContentProperty property)
    //    {
    //        TableRow tr = new TableRow();
    //        TableCell label = new TableCell();
    //        label.Text = property.DisplayName;
    //        tr.Cells.Add(label);
    //        TableCell cellControl = new TableCell();
    //        CmsPropertyControl control = new CmsPropertyControl((BaseProperty)property);

    //        cellControl.Controls.Add(control);
    //        tr.Cells.Add(cellControl);

    //        return tr;
    //    }

    //}

 
}
